<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择盘点类型" clearable>
          <el-option
            v-for="dict in bus_plan_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    
     
      <el-form-item label="审批结果" prop="approveResult">
        <el-select v-model="queryParams.approveResult" placeholder="请选择审批结果" clearable>
          <el-option
            v-for="dict in bus_plan_approve"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['crm:checkPlan:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['crm:checkPlan:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkPlanList" style="height: calc(100vh - 280px);" @selection-change="handleSelectionChange">
      <el-table-column label="ID" align="center" prop="id" width="180" fixed="left"/>
      <el-table-column label="计划名称" align="center" prop="planName" width="200" fixed="left">
		  <template #default="scope">
			 <el-link type="primary" @click="openCheckDetail(scope.row)">{{ scope.row.planName }}</el-link>
		  </template>
	  </el-table-column>
      <el-table-column label="盘点类型" align="center" prop="planType">
        <template #default="scope">
          <dict-tag :options="bus_plan_type" :value="scope.row.planType"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="approveBy" />
      <el-table-column label="审批时间" align="center" prop="approveTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approveTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" prop="approveResult" width="120">
        <template #default="scope">
          <dict-tag :options="bus_plan_approve" :value="scope.row.approveResult"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" :show-overflow-tooltip="true" prop="remark" width="120"/>
      <el-table-column label="操作" fixed="right" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="CircleCheck" v-if="!scope.row.endTime" @click="handleUpdate(scope.row)" v-hasPermi="['crm:checkPlan:edit']">完成</el-button>
          <el-button link type="primary" icon="Checked" v-if="scope.row.endTime&&!scope.row.approveTime" @click="handleApprove(scope.row)" v-hasPermi="['crm:checkPlan:edit']">审批</el-button>
		  <el-button link type="primary" icon="Delete" v-if="!scope.row.endTime" @click="handleDelete(scope.row)" v-hasPermi="['crm:checkPlan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点计划对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="checkPlanRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="盘点类型" prop="planType">
          <el-select v-model="form.planType" style="width: 100%;" placeholder="请选择盘点类型">
            <el-option
              v-for="dict in bus_plan_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable style="width: 100%;"
            v-model="form.startTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
	
	
	<!-- 添加或修改盘点计划对话框 -->
	<el-dialog title="审批计划" v-model="approveOpen" width="500px" append-to-body>
	  <el-form ref="appCheckPlanRef" :model="approveForm" :rules="approveRules" label-width="80px">
	   <el-form-item label="审批结果" prop="approveResult">
		   <el-select v-model="approveForm.approveResult" style="width: 100%;" placeholder="请选择审批结果">
		     <el-option
		       v-for="dict in bus_plan_approve"
		       :key="dict.value"
		       :label="dict.label"
		       :value="dict.value"
		     ></el-option>
		   </el-select>
	   </el-form-item>
	    <el-form-item label="备注" prop="remark">
	      <el-input v-model="approveForm.remark" type="textarea" placeholder="请输入备注" />
	    </el-form-item>
	  </el-form>
	  <template #footer>
	    <div class="dialog-footer">
	      <el-button type="primary" @click="approveSubmit">提 交</el-button>
	      <el-button @click="approveOpen=false">取 消</el-button>
	    </div>
	  </template>
	</el-dialog>
	
	<checkDetail ref="checkDetailRef" @closed="getList"/>
	
  </div>
</template>

<script setup name="CheckPlan">
import { listCheckPlan, getCheckPlan, delCheckPlan, addCheckPlan, updateCheckPlan,finishCheckPlan,approveCheckPlan } from "@/api/crm/checkPlan";
import checkDetail from "../checkDetail/index.vue"

const { proxy } = getCurrentInstance();
const { bus_plan_type, bus_plan_approve } = proxy.useDict('bus_plan_type', 'bus_plan_approve');

const checkPlanList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const approveOpen = ref(false);


const checkDetailRef = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planName: null,
    planType: null,
    startTime: null,
    endTime: null,
    belDeptId: null,
    approveBy: null,
    approveTime: null,
    approveResult: null,
  },
  rules: {
	  planName: [{
	  	required: true,
	  	message: '请输入计划名称',
	  	trigger: 'blur'
	  }],
	  planType: [{
	  	required: true,
	  	message: '请选择盘点类型',
	  	trigger: 'change'
	  }],
	  startTime:[{
		   type: 'datetime',
	  	required: true,
	  	message: '请选择开始时间',
	  	trigger: 'change'
	  }],
  },
  
  
  approveForm:{
	  id:null,
	  approveResult:null,
	  remark:null
  },
  approveRules:{
	  
  }
});

const { queryParams, form, rules,approveForm,approveRules } = toRefs(data);

/** 查询盘点计划列表 */
function getList() {
  loading.value = true;
  listCheckPlan(queryParams.value).then(response => {
    checkPlanList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    planName: null,
    planType: null,
    startTime: new Date(),
    endTime: null,
    belDeptId: null,
    approveBy: null,
    approveTime: null,
    approveResult: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("checkPlanRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加盘点计划";
}

/** 修改按钮操作 */
function handleUpdate(row) {
	proxy.$modal.confirm('是否确认完成盘点计划编号为"' + row.id + '"的数据项？').then(function() {}).then(() => {
		return finishCheckPlan(row.id);
    }).then(() => {
		proxy.$modal.msgSuccess("调整成功");
		getList();
    }).catch(() => {});
}

/**
 * 审批
 */
function handleApprove(row){
	
	approveForm.value.id = row.id;
	approveForm.value.approveResult = '1';
	approveForm.value.remark = row.remark;
	
	approveOpen.value = true;
}


function approveSubmit(){
	proxy.$refs["appCheckPlanRef"].validate(valid => {
	  if (valid) {
		approveCheckPlan(approveForm.value).then(rsp=>{
			proxy.$modal.msgSuccess("审批提交成功");
			approveOpen.value = false;
			getList();
		})
	  }
	});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkPlanRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheckPlan(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheckPlan(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除盘点计划编号为"' + row.id + '"的数据项？').then(function() {
    return delCheckPlan(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('crm/checkPlan/export', {
    ...queryParams.value
  }, `checkPlan_${new Date().getTime()}.xlsx`)
}


function openCheckDetail(row){
	checkDetailRef.value.openWin(row.id,row.planName)
}

getList();
</script>
